package com.love.marriage.userSettingsInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.userSettingsInfo.entity.UserSettingsInfo;
import com.love.marriage.userSettingsInfo.mapper.UserSettingsInfoMapper;
import com.love.marriage.userSettingsInfo.service.UserSettingsInfoService;
import com.love.marriage.userSettingsInfo.vo.UserSettingsInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  服务实现类
 * author:       mybatisPlus
 * createDate:   2024-10-12 22:22:35
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class UserSettingsInfoServiceImp extends ServiceImpl<UserSettingsInfoMapper, UserSettingsInfo> implements UserSettingsInfoService {

    private final UserSettingsInfoMapper userSettingsInfoMapper;

    @Override
    public Page<UserSettingsInfoVo> getPage(Long pageNum, Long pageSize, UserSettingsInfoVo userSettingsInfoVo) {
        Page<UserSettingsInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userSettingsInfoMapper.getPage(page, userSettingsInfoVo);
    }

    @Override
    public UserSettingsInfoVo queryUserSettingsInfo(Long id) {
        return userSettingsInfoMapper.queryUserSettingsInfo(id);
    }

    @Override
    public UserSettingsInfoVo queryUserSettingsInfoByUserId(Long userId) {
        return userSettingsInfoMapper.queryUserSettingsInfoByUserId(userId);
    }

    @Override
    public UserSettingsInfoVo addUserSettingsInfo(UserSettingsInfoVo userSettingsInfoVo) throws Exception {
        // 校验用户是否存在档案
        LambdaQueryWrapper<UserSettingsInfo> query = Wrappers.<UserSettingsInfo>lambdaQuery()
                .eq(UserSettingsInfo::getUserId, userSettingsInfoVo.getUserId())
                .eq(UserSettingsInfo::getDeleteFlag, 0);
        if (this.count(query) > 0) {
            throw new Exception("用户已存在设置，不能重复添加！");
        }
        UserSettingsInfo userSettingsInfo = new UserSettingsInfo();
        BeanUtils.copyProperties(userSettingsInfoVo, userSettingsInfo);
        userSettingsInfoMapper.insert(userSettingsInfo);
        UserSettingsInfoVo res = new UserSettingsInfoVo();
        BeanUtils.copyProperties(userSettingsInfo, res);
        return res;
    }

    @Override
    public UserSettingsInfoVo updateUserSettingsInfo(UserSettingsInfoVo userSettingsInfoVo) {
        UserSettingsInfo userSettingsInfo = new UserSettingsInfo();
        BeanUtils.copyProperties(userSettingsInfoVo, userSettingsInfo);
        userSettingsInfoMapper.updateById(userSettingsInfo);
        return userSettingsInfoVo;
    }

    @Override
    public Boolean deleteUserSettingsInfo(String ids) {
        if(StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userSettingsInfoMapper.deleteBatchIds(idArr);
        return true;
    }
}
